<?php


abstract class BaseContratacionModelo extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $id;

	
	protected $nombre;

	
	protected $fecha_adicion;

	
	protected $texto;

	
	protected $collContratacionContratos;

	
	private $lastContratacionContratoCriteria = null;

	
	protected $collContratacionModeloAVariables;

	
	private $lastContratacionModeloAVariableCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getNombre()
	{
		return $this->nombre;
	}

	
	public function getFechaAdicion($format = 'Y-m-d H:i:s')
	{

		if ($this->fecha_adicion === null) {
			return null;
		}



		try {
			$dt = new DateTime($this->fecha_adicion);
		} catch (Exception $x) {
			throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->fecha_adicion, true), $x);
		}

		if ($format === null) {
						return $dt;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $dt->format('U'));
		} else {
			return $dt->format($format);
		}
	}

	
	public function getTexto()
	{
		return $this->texto;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = ContratacionModeloPeer::ID;
		}

		return $this;
	} 
	
	public function setNombre($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->nombre !== $v) {
			$this->nombre = $v;
			$this->modifiedColumns[] = ContratacionModeloPeer::NOMBRE;
		}

		return $this;
	} 
	
	public function setFechaAdicion($v)
	{
		if ($v === null) {
			$dt = null;
		} elseif ($v instanceof DateTime) {
			$dt = $v;
		} else {
									try {
				if (is_numeric($v)) { 					$dt = new DateTime('@'.$v, new DateTimeZone('UTC'));
															$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
				} else {
					$dt = new DateTime($v);
				}
			} catch (Exception $x) {
				throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x);
			}
		}

		if ( $this->fecha_adicion !== null || $dt !== null ) {
			
			$currNorm = ($this->fecha_adicion !== null && $tmpDt = new DateTime($this->fecha_adicion)) ? $tmpDt->format('Y-m-d\\TH:i:sO') : null;
			$newNorm = ($dt !== null) ? $dt->format('Y-m-d\\TH:i:sO') : null;

			if ( ($currNorm !== $newNorm) 					)
			{
				$this->fecha_adicion = ($dt ? $dt->format('Y-m-d\\TH:i:sO') : null);
				$this->modifiedColumns[] = ContratacionModeloPeer::FECHA_ADICION;
			}
		} 
		return $this;
	} 
	
	public function setTexto($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->texto !== $v) {
			$this->texto = $v;
			$this->modifiedColumns[] = ContratacionModeloPeer::TEXTO;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->nombre = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->fecha_adicion = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->texto = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 4; 
		} catch (Exception $e) {
			throw new PropelException("Error populating ContratacionModelo object", $e);
		}
	}

	
	public function ensureConsistency()
	{

	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(ContratacionModeloPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = ContratacionModeloPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->collContratacionContratos = null;
			$this->lastContratacionContratoCriteria = null;

			$this->collContratacionModeloAVariables = null;
			$this->lastContratacionModeloAVariableCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(ContratacionModeloPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
			$con->beginTransaction();
			ContratacionModeloPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(ContratacionModeloPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
		    if($this->validate())
		    {
    			$con->beginTransaction();
    			$affectedRows = $this->doSave($con);
    			$con->commit();
    			ContratacionModeloPeer::addInstanceToPool($this);
    			return true;
			}else
			{
			     
                 return false;
            }
		} catch (PropelException $e) {
			$con->rollback();
			
			
			ErrorLog::reportarError('errors', 'error',  $e->getMessage() );
			return false;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = ContratacionModeloPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += ContratacionModeloPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			if ($this->collContratacionContratos !== null) {
				foreach ($this->collContratacionContratos as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collContratacionModeloAVariables !== null) {
				foreach ($this->collContratacionModeloAVariables as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = ContratacionModeloPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collContratacionContratos !== null) {
					foreach ($this->collContratacionContratos as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collContratacionModeloAVariables !== null) {
					foreach ($this->collContratacionModeloAVariables as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = ContratacionModeloPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getNombre();
				break;
			case 2:
				return $this->getFechaAdicion();
				break;
			case 3:
				return $this->getTexto();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = ContratacionModeloPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getNombre(),
			$keys[2] => $this->getFechaAdicion(),
			$keys[3] => $this->getTexto(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = ContratacionModeloPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setNombre($value);
				break;
			case 2:
				$this->setFechaAdicion($value);
				break;
			case 3:
				$this->setTexto($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = ContratacionModeloPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setNombre($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setFechaAdicion($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setTexto($arr[$keys[3]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);

		if ($this->isColumnModified(ContratacionModeloPeer::ID)) $criteria->add(ContratacionModeloPeer::ID, $this->id);
		if ($this->isColumnModified(ContratacionModeloPeer::NOMBRE)) $criteria->add(ContratacionModeloPeer::NOMBRE, $this->nombre);
		if ($this->isColumnModified(ContratacionModeloPeer::FECHA_ADICION)) $criteria->add(ContratacionModeloPeer::FECHA_ADICION, $this->fecha_adicion);
		if ($this->isColumnModified(ContratacionModeloPeer::TEXTO)) $criteria->add(ContratacionModeloPeer::TEXTO, $this->texto);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);

		$criteria->add(ContratacionModeloPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setNombre($this->nombre);

		$copyObj->setFechaAdicion($this->fecha_adicion);

		$copyObj->setTexto($this->texto);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getContratacionContratos() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addContratacionContrato($relObj->copy($deepCopy));
				}
			}

			foreach ($this->getContratacionModeloAVariables() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addContratacionModeloAVariable($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new ContratacionModeloPeer();
		}
		return self::$peer;
	}

	
	public function clearContratacionContratos()
	{
		$this->collContratacionContratos = null; 	}

	
	public function getContratacionContratos($criteria = null, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collContratacionContratos === null) {
			if ($this->isNew()) {
			   $this->collContratacionContratos = array();
			} else {

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				ContratacionContratoPeer::addSelectColumns($criteria);
				$this->collContratacionContratos = ContratacionContratoPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				ContratacionContratoPeer::addSelectColumns($criteria);
				if (!isset($this->lastContratacionContratoCriteria) || !$this->lastContratacionContratoCriteria->equals($criteria)) {
					$this->collContratacionContratos = ContratacionContratoPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastContratacionContratoCriteria = $criteria;
		return $this->collContratacionContratos;
	}

	
	public function countContratacionContratos(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collContratacionContratos === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				$count = ContratacionContratoPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				if (!isset($this->lastContratacionContratoCriteria) || !$this->lastContratacionContratoCriteria->equals($criteria)) {
					$count = ContratacionContratoPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collContratacionContratos);
				}
			} else {
				$count = count($this->collContratacionContratos);
			}
		}
		$this->lastContratacionContratoCriteria = $criteria;
		return $count;
	}

	
	public function addContratacionContrato(ContratacionContrato $l)
	{
		if ($this->collContratacionContratos === null) {
			$this->collContratacionContratos = array();
		}
		if (!in_array($l, $this->collContratacionContratos, true)) { 			array_push($this->collContratacionContratos, $l);
			$l->setContratacionModelo($this);
		}
	}


	
	public function getContratacionContratosJoinKernelTercero($criteria = null, $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collContratacionContratos === null) {
			if ($this->isNew()) {
				$this->collContratacionContratos = array();
			} else {

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				$this->collContratacionContratos = ContratacionContratoPeer::doSelectJoinKernelTercero($criteria, $con);
			}
		} else {
									
			$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

			if (!isset($this->lastContratacionContratoCriteria) || !$this->lastContratacionContratoCriteria->equals($criteria)) {
				$this->collContratacionContratos = ContratacionContratoPeer::doSelectJoinKernelTercero($criteria, $con);
			}
		}
		$this->lastContratacionContratoCriteria = $criteria;

		return $this->collContratacionContratos;
	}


	
	public function getContratacionContratosJoinKernelUsuario($criteria = null, $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collContratacionContratos === null) {
			if ($this->isNew()) {
				$this->collContratacionContratos = array();
			} else {

				$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

				$this->collContratacionContratos = ContratacionContratoPeer::doSelectJoinKernelUsuario($criteria, $con);
			}
		} else {
									
			$criteria->add(ContratacionContratoPeer::MODELO_ID, $this->getId());

			if (!isset($this->lastContratacionContratoCriteria) || !$this->lastContratacionContratoCriteria->equals($criteria)) {
				$this->collContratacionContratos = ContratacionContratoPeer::doSelectJoinKernelUsuario($criteria, $con);
			}
		}
		$this->lastContratacionContratoCriteria = $criteria;

		return $this->collContratacionContratos;
	}

	
	public function clearContratacionModeloAVariables()
	{
		$this->collContratacionModeloAVariables = null; 	}

	
	public function getContratacionModeloAVariables($criteria = null, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collContratacionModeloAVariables === null) {
			if ($this->isNew()) {
			   $this->collContratacionModeloAVariables = array();
			} else {

				$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

				ContratacionModeloAVariablePeer::addSelectColumns($criteria);
				$this->collContratacionModeloAVariables = ContratacionModeloAVariablePeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

				ContratacionModeloAVariablePeer::addSelectColumns($criteria);
				if (!isset($this->lastContratacionModeloAVariableCriteria) || !$this->lastContratacionModeloAVariableCriteria->equals($criteria)) {
					$this->collContratacionModeloAVariables = ContratacionModeloAVariablePeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastContratacionModeloAVariableCriteria = $criteria;
		return $this->collContratacionModeloAVariables;
	}

	
	public function countContratacionModeloAVariables(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collContratacionModeloAVariables === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

				$count = ContratacionModeloAVariablePeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

				if (!isset($this->lastContratacionModeloAVariableCriteria) || !$this->lastContratacionModeloAVariableCriteria->equals($criteria)) {
					$count = ContratacionModeloAVariablePeer::doCount($criteria, $con);
				} else {
					$count = count($this->collContratacionModeloAVariables);
				}
			} else {
				$count = count($this->collContratacionModeloAVariables);
			}
		}
		$this->lastContratacionModeloAVariableCriteria = $criteria;
		return $count;
	}

	
	public function addContratacionModeloAVariable(ContratacionModeloAVariable $l)
	{
		if ($this->collContratacionModeloAVariables === null) {
			$this->collContratacionModeloAVariables = array();
		}
		if (!in_array($l, $this->collContratacionModeloAVariables, true)) { 			array_push($this->collContratacionModeloAVariables, $l);
			$l->setContratacionModelo($this);
		}
	}


	
	public function getContratacionModeloAVariablesJoinContratacionVariable($criteria = null, $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(ContratacionModeloPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collContratacionModeloAVariables === null) {
			if ($this->isNew()) {
				$this->collContratacionModeloAVariables = array();
			} else {

				$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

				$this->collContratacionModeloAVariables = ContratacionModeloAVariablePeer::doSelectJoinContratacionVariable($criteria, $con);
			}
		} else {
									
			$criteria->add(ContratacionModeloAVariablePeer::MODELO_ID, $this->getId());

			if (!isset($this->lastContratacionModeloAVariableCriteria) || !$this->lastContratacionModeloAVariableCriteria->equals($criteria)) {
				$this->collContratacionModeloAVariables = ContratacionModeloAVariablePeer::doSelectJoinContratacionVariable($criteria, $con);
			}
		}
		$this->lastContratacionModeloAVariableCriteria = $criteria;

		return $this->collContratacionModeloAVariables;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			foreach($this->collContratacionContratos as $o) {
				$o->clearAllReferences($deep);
			}
			foreach($this->collContratacionModeloAVariables as $o) {
				$o->clearAllReferences($deep);
			}
		} 
		$this->collContratacionContratos = null;
		$this->collContratacionModeloAVariables = null;
	}

} 